<?php

namespace App\Http\Controllers\Admin;

use Admin;
use App\Http\Controllers\Controller;
use App\Http\Requests\PermissionRequest;
use Illuminate\Http\Request;
use Spatie\Permission\Models\Permission;

class PermissionController extends Controller
{
    public function index(Request $request, Permission $perm)
    {
        $content = Admin::content()
                        ->title('权限')
                        ->desc('所有权限')
                        ->content($this->table());

        return response()->json([
            'status'  => true,
            'content' => $content,
        ]);
    }

    protected function table()
    {
        $table = Admin::table(Permission::class)
                      ->columns([
                          'id'           => '编号',
                          'display_name' => '权限名',
                          'name'         => '标志',
                          'created_at'   => '添加时间',
                          'updated_at'   => '最后更新',
                      ]);

        return $table;
    }

    public function create(Request $request, Permission $perm)
    {
        $content = Admin::content()
                        ->title('权限')
                        ->desc('添加权限')
                        ->content($this->form($perm));

        return response()->json([
            'status'  => true,
            'content' => $content,
        ]);
    }

    public function edit(Request $request, Permission $perm)
    {
        $content = Admin::content()
                        ->title('权限')
                        ->desc('编辑权限')
                        ->content($this->form($perm)->edit());

        return response()->json([
            'status'  => true,
            'content' => $content,
        ]);
    }

    protected function form(Permission $perm = null)
    {
        $form = Admin::form($perm);

        $form->input('display_name', '权限名')->required();
        $form->input('name', '标志')->required();

        return $form;
    }

    public function store(PermissionRequest $request, Permission $perm)
    {
        return $this->form($perm)->store();
    }

    public function destroy(Permission $perm)
    {
        return $this->form($perm)->destroy();
    }

    public function update(PermissionRequest $request, Permission $perm)
    {
        return $this->form($perm)->update();
    }
}
